package org.tis.tools.abf.module.ac.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.*;
import org.tis.tools.abf.module.ac.entity.AcOperatorRole;
import org.tis.tools.abf.module.ac.service.IAcOperatorRoleService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acOperatorRole的Controller类
 * 
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@Validated
@RequestMapping("/acOperatorRole")
@Slf4j
public class AcOperatorRoleController extends BaseController<AcOperatorRole>  {

    @Autowired
    private IAcOperatorRoleService acOperatorRoleService;

    @ApiOperation(value = "增加操作员与权限集（角色）对应关系",notes = "传入AcOperatorRole类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "增加操作员与权限集（角色）对应关系")
    @PostMapping("/add")
    public ResultVO add(@RequestBody @Validated({AcOperatorRoleAddGrop.class}) @ApiParam(value = "acOperatorRole",name = "传入的AcOperatorRole类",required = true) AcOperatorRole acOperatorRole) {
        acOperatorRoleService.insert(acOperatorRole);
        EntityWrapper<AcOperatorRole> acOperatorRoleEntityWrapper = new EntityWrapper<>();
        acOperatorRoleEntityWrapper.eq(AcOperatorRole.COLUMN_GUID_OPERATOR,acOperatorRole.getGuidOperator());
        acOperatorRoleEntityWrapper.eq(AcOperatorRole.COLUMN_GUID_ROLE,acOperatorRole.getGuidRole());
        AcOperatorRole acOperatorRole1 = acOperatorRoleService.selectOne(acOperatorRoleEntityWrapper);
        return ResultVO.success("新增成功！",acOperatorRole1);
    }


    @ApiOperation(value = "修改操作员与权限集（角色）对应关系",notes = "传入AcOperatorRole类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改操作员与权限集（角色）对应关系")
    @PutMapping
    public ResultVO update(@RequestBody @Validated({AcOPeratorRoleUpdateGrop.class}) @ApiParam(value = "acOperatorRole",name = "传入的AcOperatorRole类",required = true) AcOperatorRole acOperatorRole) {
        acOperatorRoleService.updateById(acOperatorRole);
        AcOperatorRole acOperatorRole1 = acOperatorRoleService.selectById(acOperatorRole.getGuid());
        return ResultVO.success("修改成功！",acOperatorRole1);
    }


    @ApiOperation(value = "删除操作员与权限集（角色）对应关系",notes = "传入操作员与权限集GUID作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除操作员与权限集（角色）对应关系")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空")  @ApiParam(value = "id",name = "操作员与权限集GUID",required = true) String id) {
        AcOperatorRole acOperatorRole = acOperatorRoleService.selectById(id);
        if (acOperatorRole == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acOperatorRoleService.deleteOperatorRole(id);
        return ResultVO.success("删除成功",acOperatorRole);
    }


    @ApiOperation(value = "根据操作员和角色ID删除操作员与权限集（角色）对应关系",notes = "传入AcDeleteRoleOperatorListRequest类作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "根据操作员和角色ID删除操作员与权限集（角色）对应关系")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "AcDeleteRoleOperatorListRequest",name = "传入的AcDeleteRoleOperatorListRequest类",required = true) AcDeleteRoleOperatorListRequest listRequest) {
        acOperatorRoleService.batchDelete(listRequest);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据GUID查询操作员与权限集(角色)对应关系",notes = "传入操作员与权限集GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员与权限集GUID",required = true) String id) {
        AcOperatorRole acOperatorRole = acOperatorRoleService.selectById(id);
        if (acOperatorRole == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acOperatorRole);
    }

    @ApiOperation(value = "分页查询操作员与权限集(角色)对应关系",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息",required = true) SmartPage<AcOperatorRole> page) {
        return  ResultVO.success("查询成功", acOperatorRoleService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "为角色分配操作员",notes = "传入AcOperatorRoleBatchAddRequest类作为参数")
    @PostMapping("/batchAddOperator")
    public ResultVO batchAdd(@RequestBody @Validated @ApiParam(value = "AcOperatorRoleBatchAddRequest",name = "传入的AcOperatorRoleBatchAddRequest类",required = true) AcOperatorRoleBatchAddRequest batchAddRequest){
        acOperatorRoleService.batchAdd(batchAddRequest.getRoleId(),batchAddRequest.getList());
        return ResultVO.success("新增成功");
    }


    @ApiOperation(value = "为操作员分配角色",notes = "传入AcRoleOperatorBatchAddRequest类作为参数")
    @PostMapping("/batchAddRole")
    public ResultVO batchAddRole(@RequestBody @Validated @ApiParam(value = "AcRoleOperatorBatchAddRequest",name = "传入的AcRoleOperatorBatchAddRequest类",required = true) AcRoleOperatorBatchAddRequest batchAddRequest){
        acOperatorRoleService.batchAddRole(batchAddRequest);
        return ResultVO.success("新增成功");
    }


    @ApiOperation(value = "查询未分配角色和已绑定角色",notes = "传入操作员的USERID作为参数")
    @GetMapping("/queryRoleByOperator/{userId}")
    public ResultVO queryRoleByOperator(@PathVariable @NotBlank(message = "userId不能为空") @ApiParam(value = "userId",name = "操作员的USERID",required = true) String userId){
        return ResultVO.success("查询成功",acOperatorRoleService.queryRoleByOperator(userId));
    }


    @ApiOperation(value = "查询未分配操作员和已绑定操作员",notes = "传入角色GUID作为参数")
    @GetMapping("/queryOperatorByRole/{roleId}")
    public ResultVO queryOperatorByRole(@PathVariable @NotBlank(message = "roleId不能为空") @ApiParam(value = "roleId",name = "角色的GUID",required = true) String roleId){
        return ResultVO.success("查询成功",acOperatorRoleService.queryOperatorByRole(roleId));
    }
}

